package zero.tech.games.log.hot;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import zero.tech.core.data.PageBean;
import zero.tech.games.api.RemoteUser;
import zero.tech.games.common.RedisConsts;
import zero.tech.games.entity.log.LogWaterTeam;
import zero.tech.games.entity.user.TUser;
import zero.tech.games.log.data.TmpEntity;
import zero.tech.games.log.data.WaterTeamInfo;
import zero.tech.games.log.data.res.ResLogZsTeamWater;
import zero.tech.games.log.mapper.LogWaterTeamMapper;
import zero.tech.redis.cache.RedisUtil;

@Component
public class LogWaterTeamUtil {
	@Autowired
	private RedisUtil redisUtil;
	@Autowired
	private RemoteUser remoteUser;
	@Autowired
	private LogWaterTeamMapper logWaterTeamMapper;
	@SuppressWarnings("unchecked")
	public PageBean<WaterTeamInfo> listWaterTeamByPage(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<WaterTeamInfo> pageBean=(PageBean<WaterTeamInfo>) redisUtil.hget(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logWaterTeamMapper.totalCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<WaterTeamInfo> result=new ArrayList<WaterTeamInfo>();
			List<LogWaterTeam> logWaterTeams=logWaterTeamMapper.listWaterTeamByPage(club_id,uid,start,pageLength,begin,end);
			if(!logWaterTeams.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					if(!uids.contains(logTeamInfo.getUid())) {
						uids.add(logTeamInfo.getUid());
					}
					if(!uids.contains(logTeamInfo.getOther_uid())) {
						uids.add(logTeamInfo.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					result.add(packageWaterTeamInfo(logTeamInfo,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	private WaterTeamInfo packageWaterTeamInfo(LogWaterTeam logTeamInfo, Map<Integer, TUser> usermap) {
		TUser user = usermap.get(logTeamInfo.getUid());
		TUser other=usermap.get(logTeamInfo.getOther_uid());
		WaterTeamInfo waterTeamInfo =new WaterTeamInfo();
		waterTeamInfo.setClub_id(logTeamInfo.getClub_id());
		waterTeamInfo.setDirect(logTeamInfo.getDirect());
		waterTeamInfo.setOther_uid(logTeamInfo.getOther_uid());
		waterTeamInfo.setUid(logTeamInfo.getUid());
		waterTeamInfo.setWater(logTeamInfo.getWater());
		waterTeamInfo.setWater_effect(logTeamInfo.getWater_effect());
		waterTeamInfo.setDay_str(logTeamInfo.getDay_str());
		waterTeamInfo.setCreate_stamp(logTeamInfo.getCreate_stamp());
		waterTeamInfo.setNick(user.getNick());
		waterTeamInfo.setHead_img(user.getHead_img());
		waterTeamInfo.setOther_nick(other.getNick());
		waterTeamInfo.setOther_head_img(other.getHead_img());
		waterTeamInfo.setGame_id(logTeamInfo.getGame_id());
		waterTeamInfo.setRoom_id(logTeamInfo.getRoom_id());
		waterTeamInfo.setRule_id(logTeamInfo.getRule_id());
		return waterTeamInfo;
	}
	public void insertBatch(List<LogWaterTeam> logWaterTeams) {
		logWaterTeamMapper.insertBatch(logWaterTeams);
		List<String> list=new ArrayList<String>();
		for(LogWaterTeam logWaterTeam:logWaterTeams) {
			int club_id=logWaterTeam.getClub_id();
			int uid=logWaterTeam.getUid();
			String str=club_id+"_"+uid;
			if(!list.contains(str)) {
				list.add(str);
			}
		}
		for(String str:list) {
			redisUtil.del(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_WATER_TEAM_ZS_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_WATER_TEAM_FZS_PAGE+"_"+str);
			redisUtil.del(RedisConsts.HOT_LOG_WATER_ZSTEAM_BY_PAGE+"_"+str);
		}		
	}
	@SuppressWarnings("unchecked")
	public PageBean<WaterTeamInfo> listWaterTeamZsByPage(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<WaterTeamInfo> pageBean=(PageBean<WaterTeamInfo>) redisUtil.hget(RedisConsts.HOT_LOG_WATER_TEAM_ZS_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logWaterTeamMapper.totalZsCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<WaterTeamInfo> result=new ArrayList<WaterTeamInfo>();
			List<LogWaterTeam> logWaterTeams=logWaterTeamMapper.listWaterTeamZsByPage(club_id,uid,start,pageLength,begin,end);
			if(!logWaterTeams.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					if(!uids.contains(logTeamInfo.getUid())) {
						uids.add(logTeamInfo.getUid());
					}
					if(!uids.contains(logTeamInfo.getOther_uid())) {
						uids.add(logTeamInfo.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					result.add(packageWaterTeamInfo(logTeamInfo,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_ZS_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	@SuppressWarnings("unchecked")
	public PageBean<WaterTeamInfo> listWaterTeamFzsByPage(int uid, int club_id, int page, int pageLength, Date begin,
			Date end) {
		PageBean<WaterTeamInfo> pageBean=(PageBean<WaterTeamInfo>) redisUtil.hget(RedisConsts.HOT_LOG_WATER_TEAM_FZS_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime());
		if(pageBean==null) {
			int totalCount=logWaterTeamMapper.totalFzsCount(club_id,uid,begin,end);
			int start=page*pageLength;
			List<WaterTeamInfo> result=new ArrayList<WaterTeamInfo>();
			List<LogWaterTeam> logWaterTeams=logWaterTeamMapper.listWaterTeamFzsByPage(club_id,uid,start,pageLength,begin,end);
			if(!logWaterTeams.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					if(!uids.contains(logTeamInfo.getUid())) {
						uids.add(logTeamInfo.getUid());
					}
					if(!uids.contains(logTeamInfo.getOther_uid())) {
						uids.add(logTeamInfo.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					result.add(packageWaterTeamInfo(logTeamInfo,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_FZS_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), pageBean,5*60);
		}
		return pageBean;
	}
	public ResLogZsTeamWater waterZsTeamLogs(int club_id, int uid, Date begin, Date end, int page, int pageLength) {
		ResLogZsTeamWater resLogZsTeamWater=(ResLogZsTeamWater) redisUtil.hget(RedisConsts.HOT_LOG_WATER_ZSTEAM_BY_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime() );
		if(resLogZsTeamWater==null) {
			resLogZsTeamWater=new ResLogZsTeamWater();
			PageBean<WaterTeamInfo> pageBean=listWaterTeamZsByPage(uid,club_id,page,pageLength,begin,end);
			TmpEntity tmpEntity=logWaterTeamMapper.totalZsWater(club_id,uid,begin,end);
			long total_water=tmpEntity.getTotal_water();
			long total_water_effect=tmpEntity.getTotal_water_effect();
			resLogZsTeamWater.setTotal_water(total_water);
			resLogZsTeamWater.setTotal_water_effect(total_water_effect);
			resLogZsTeamWater.setPageBean(pageBean);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_ZSTEAM_BY_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime(), resLogZsTeamWater,5*60);
		}
		return resLogZsTeamWater;
	}
	public ResLogZsTeamWater waterZsTeamLogsByUid(int club_id, int uid, Date begin, Date end, int page, int pageLength,
			Integer other_uid) {
		ResLogZsTeamWater resLogZsTeamWater=(ResLogZsTeamWater) redisUtil.hget(RedisConsts.HOT_LOG_WATER_ZSTEAM_BY_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid );
		if(resLogZsTeamWater==null) {
			resLogZsTeamWater=new ResLogZsTeamWater();
			PageBean<WaterTeamInfo> pageBean=listWaterTeamZsByPageAndUid(uid,club_id,page,pageLength,begin,end,other_uid);
			TmpEntity tmpEntity=logWaterTeamMapper.totalZsWater(club_id,uid,begin,end);
			long total_water=tmpEntity.getTotal_water();
			long total_water_effect=tmpEntity.getTotal_water_effect();
			resLogZsTeamWater.setTotal_water(total_water);
			resLogZsTeamWater.setTotal_water_effect(total_water_effect);
			resLogZsTeamWater.setPageBean(pageBean);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_ZSTEAM_BY_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid, resLogZsTeamWater,5*60);
		}
		return resLogZsTeamWater;
	}
	@SuppressWarnings("unchecked")
	private PageBean<WaterTeamInfo> listWaterTeamZsByPageAndUid(int uid, int club_id, int page, int pageLength,
			Date begin, Date end, Integer other_uid) {
		PageBean<WaterTeamInfo> pageBean=(PageBean<WaterTeamInfo>) redisUtil.hget(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid);
		if(pageBean==null) {
			int totalCount=logWaterTeamMapper.totalZsCountByUid(club_id,uid,begin,end,other_uid);
			int start=page*pageLength;
			List<WaterTeamInfo> result=new ArrayList<WaterTeamInfo>();
			List<LogWaterTeam> logWaterTeams=logWaterTeamMapper.listWaterTeamByPageAndUid(club_id,uid,start,pageLength,begin,end,other_uid);
			if(!logWaterTeams.isEmpty()) {
				List<Integer> uids=new ArrayList<>();
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					if(!uids.contains(logTeamInfo.getUid())) {
						uids.add(logTeamInfo.getUid());
					}
					if(!uids.contains(logTeamInfo.getOther_uid())) {
						uids.add(logTeamInfo.getOther_uid());
					}
				}
				Map<Integer,TUser> usermap=new HashMap<>();
				List<TUser> users=remoteUser.getUserBatch(uids);
				for(TUser user:users) {
					usermap.put(user.getUid(), user);
				}
				for(LogWaterTeam logTeamInfo:logWaterTeams) {
					result.add(packageWaterTeamInfo(logTeamInfo,usermap));
				}
			}
			pageBean = new PageBean<>(totalCount, pageLength, page, result);
			redisUtil.hset(RedisConsts.HOT_LOG_WATER_TEAM_PAGE+"_"+club_id+"_"+uid,page+"_"+pageLength+"_"+begin.getTime()+"_"+end.getTime()+"_"+other_uid, pageBean,5*60);
		}
		return pageBean;
	}
}
